<?php
class StoreApp extends StorebaseApp {
	function index() {
		/* 店铺信息 */
		$_GET['act'] = 'index';
		$id = empty ($_GET['id']) ? 0 : intval($_GET['id']);
		$cate_id = empty ($_GET['cate_id']) ? 0 : intval($_GET['cate_id']); //菜品分类id
		if (!$id) {
			$this->show_warning('Hacking Attempt');
			return;
		}
		//查询店铺名称
		$store_mod = & m('store');
		$store_name = $store_mod->get_store($id);
		$this->assign('store_name', $store_name[0]['store_name']);
		$this->assign('store_id', $store_name[0]['store_id']);
		//取得店铺菜品分类
		$store_mod = & m('store');
		$store_array = $store_mod->get_storecate($id);
		//        $this->assign("store_list", $store_array);

		//取得全部分类的菜品
		$n = 0;
		$goods_list["$n"] = $store_mod->get_storecategoods($id, "");
		$this->assign("goods_list$n", $goods_list["$n"]);

		for ($i = 0; $i < count($store_array); $i++) { //取得店铺菜系分类中的所有菜品
		    $this->assign("cate_listid$i", $store_array[$i]['cate_id']);
			$goods_list["$i"] = $store_mod->get_storecategoods($store_name[0]['store_id'], $store_array[$i]['cate_id']);
			$ii = $i +1;
			$this->assign("goods_list$ii", $goods_list["$i"]);
		}
		$res = array (
			"cate_id" => "0",
			"store_id" => "0",
			"cate_name" => "全部分类"
		);
		$dataarray = $store_array;
		array_unshift($dataarray, $res);
		if(empty($cate_id)){
           $this->assign("gcategory_id", 0);
		}else{
		   $this->assign("gcategory_id", $cate_id);
		}
		$this->assign("store_all", $dataarray);

		$this->set_store($id);
		$store = $this->get_store_data();
		$this->assign('store', $store);

		/* 取得友情链接 */
		$this->assign('partners', $this->_get_partners($id));

		/* 取得推荐商品 */
		$this->assign('recommended_goods', $this->_get_recommended_goods($id));
		$this->assign('recommended_goods_id', $id);
		//        $this->assign('new_groupbuy', $this->_get_new_groupbuy($id));

		/* 取得最新商品 */
		$this->assign('new_goods', $this->_get_new_goods($id));
		$this->assign('new_goods_id', $id);
		/* 取得特价商品 */
		$this->assign('new_recommended_goods', $this->_get_newt_recommended_goods($id));
		$this->assign('new_recommended_goods_id', $id);
		$this->assign('show_chaining', 1);
		/*提取店铺大图片$id*/
        $store_name = $store_mod->get_storelogimg($id);
        $this->assign('storelogimg', $store_name[0]['file_path']);
        /*当前路径*/
        $propath=dirname($_SERVER['PHP_SELF']);
//		list($path1,$path2,$path3)=explode("/",$propath);
 		$this->assign("prorootpath", $propath);
		/* 当前位置 */
		$this->_curlocal(LANG :: get('all_stores'), 'index.php?app=search&amp;act=store', $store['store_name']);

		$this->_config_seo('title', $store['store_name'] . ' - ' . Conf :: get('site_title'));
		/* 配置seo信息 */
		$this->_config_seo($this->_get_seo_info($store));
		$this->display('store.index.html');
	}

	function search() {
		/* 店铺信息 */
		$id = empty ($_GET['id']) ? 0 : intval($_GET['id']); //店铺编号
		$cate_id = empty ($_GET['cate_id']) ? 0 : intval($_GET['cate_id']); //店铺分类编号
		if (!$id) {
			$this->show_warning('Hacking Attempt');
			return;
		}
		$this->set_store($id);
		$store = $this->get_store_data();
		$this->assign('store', $store);
		$this->assign('cate_id', $cate_id);
		/* 搜索到的商品 */
//		if (empty ($cate_id)) { //搜索店铺中的所有菜品
			$this->_assign_searched_goods($id, $cate_id);
//		} else { //搜索店铺中指定类型的菜品
//			$this->_assign_searched_goods($id, $cate_id);
//		}
        $store_mod = & m('store');
		$store_array = $store_mod->get_storecate($id);
		$res = array (
			"cate_id" => "0",
			"store_id" => "0",
			"cate_name" => "全部分类"
		);
        $dataarray = $store_array; /*当前分类*/
		array_unshift($dataarray, $res);
		$this->assign("gcategory_id", 0);
		$this->assign("store_all", $dataarray);
		$store_name = $store_mod->get_store($id);
		$this->assign('store_name', $store_name[0]['store_name']);
		$this->assign('store_id', $store_name[0]['store_id']);
        $this->assign('show_chaining', 0);
		/* 当前位置 */
		$this->_curlocal(LANG :: get('all_stores'), 'index.php?app=search&amp;act=store', $store['store_name'], 'index.php?app=store&amp;id=' . $store['store_id'], LANG :: get('goods_list'));

		$this->_config_seo('title', Lang :: get('goods_list') . ' - ' . $store['store_name']);
		$this->display('store.search.html');
	}

	function search_goods() {
		$new_goods_storeid = empty ($_GET['new_goods_storeid']) ? 0 : intval($_GET['new_goods_storeid']); //新品推荐
		$recommended_goods_id = empty ($_GET['recommended_goods_id']) ? 0 : intval($_GET['recommended_goods_id']); //精品特色
		$new_recommended_goods_id = empty ($_GET['new_recommended_goods_id']) ? 0 : intval($_GET['new_recommended_goods_id']); //今日特价

		if (!empty ($new_goods_storeid)) { /* 取得最新商品 */
			$id=$new_goods_storeid;
			$this->assign('searched_goods', $this->_get_new_goods($new_goods_storeid,50));
            $this->assign('show_cate_new_recommend', 1);
            $this->assign('search_name', "新品推荐");
		}
		if (!empty ($recommended_goods_id)) { /* 取得推荐商品 */
            $id=$recommended_goods_id;
			$this->assign('searched_goods', $this->_get_recommended_goods($recommended_goods_id,50));
			$this->assign('show_cate_recommended', 1);
			$this->assign('search_name', "精品特色");
		}
		if (!empty ($new_recommended_goods_id)) { /*取得今日特价*/
			$id=$new_recommended_goods_id;
			$this->assign('searched_goods', $this->_get_newt_recommended_goods($new_recommended_goods_id,50));
			$this->assign('show_cate_new_special_offer', 1);
			$this->assign('search_name', "今日特价");
		}

		$store_mod = & m('store');
		$store_array = $store_mod->get_storecate($id);
		$res = array (
			"cate_id" => "0",
			"store_id" => "0",
			"cate_name" => "全部分类"
		);
		$dataarray = $store_array; /*当前分类*/
		array_unshift($dataarray, $res);
		$this->assign("gcategory_id", 0);
		$this->assign("store_all", $dataarray);
		$store_name = $store_mod->get_store($id);
		$this->assign('store_name', $store_name[0]['store_name']);
		$this->assign('store_id', $store_name[0]['store_id']);
		$this->assign('show_chaining', 0);
		/* 当前位置 */
        $this->set_store($id);
		$store = $this->get_store_data();
		$this->_curlocal(LANG :: get('all_stores'), 'index.php?app=search&amp;act=store', $store['store_name'], 'index.php?app=store&amp;id=' . $store['store_id'], LANG :: get('goods_list'));
		$this->_config_seo('title', Lang :: get('goods_list') . ' - ' . $store['store_name']);
		$this->assign('searched_search_goods', 1);
		$this->display('store.search.html');
	}

    function search_categoods(){
       $allcateid = empty ($_GET['allcateid']) ? 0 : intval($_GET['allcateid']); //全部分类
       $goods_list = $store_mod->get_allstorecategoods($allcateid, "");
	   $this->assign("goods_list", $goods_list);
    }


	function groupbuy() {
		/* 店铺信息 */
		$id = empty ($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('Hacking Attempt');
			return;
		}
		$this->set_store($id);
		$store = $this->get_store_data();
		$this->assign('store', $store);

		/* 搜索团购 */
		empty ($_GET['state']) && $_GET['state'] = 'on';
		$conditions = '1=1';
		if ($_GET['state'] == 'on') {
			$conditions .= ' AND gb.state =' . GROUP_ON . ' AND gb.end_time>' . gmtime();
			$search_name = array (
				array (
					'text' => Lang :: get('group_on'
				)
			), array (
				'text' => Lang :: get('all_groupbuy'
			), 'url' => url('app=store&act=groupbuy&state=all&id=' . $id)),);
		} else
			if ($_GET['state'] == 'all') {
				$conditions .= ' AND gb.state ' . db_create_in(array (
					GROUP_ON,
					GROUP_END,
					GROUP_FINISHED
				));
				$search_name = array (
					array (
						'text' => Lang :: get('all_groupbuy'
					)
				), array (
					'text' => Lang :: get('group_on'
				), 'url' => url('app=store&act=groupbuy&state=on&id=' . $id)),);
			}

		$page = $this->_get_page(16);
		$groupbuy_mod = & m('groupbuy');
		$groupbuy_list = $groupbuy_mod->find(array (
			'fields' => 'goods.default_image, gb.group_name, gb.group_id, gb.spec_price, gb.end_time, gb.state',
			'join' => 'belong_goods',
			'conditions' => $conditions . ' AND gb.store_id=' . $id,
			'order' => 'group_id DESC',
			'limit' => $page['limit'],
			'count' => true
		));
		$page['item_count'] = $groupbuy_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		if (empty ($groupbuy_list)) {
			$groupbuy_list = array ();
		}
		foreach ($groupbuy_list as $key => $_g) {
			empty ($groupbuy_list[$key]['default_image']) && $groupbuy_list[$key]['default_image'] = Conf :: get('default_goods_image');
			$tmp = current(unserialize($_g['spec_price']));
			$groupbuy_list[$key]['price'] = $tmp['price'];
			if ($_g['end_time'] < gmtime()) {
				$groupbuy_list[$key]['group_state'] = group_state($_g['state']);
			} else {
				$groupbuy_list[$key]['lefttime'] = lefttime($_g['end_time']);
			}
		}
		/* 当前位置 */
		$this->_curlocal(LANG :: get('all_stores'), 'index.php?app=search&amp;act=store', $store['store_name'], 'index.php?app=store&amp;id=' . $store['store_id'], LANG :: get('groupbuy_list'));

		$this->assign('groupbuy_list', $groupbuy_list);
		$this->assign('search_name', $search_name);
		$this->_config_seo('title', $search_name[0]['text'] . ' - ' . $store['store_name']);
		$this->display('store.groupbuy.html');
	}

	function article() {
		$id = empty ($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('Hacking Attempt');
			return;
		}
		$article = $this->_get_article($id);
		if (!$article) {
			$this->show_warning('Hacking Attempt');
			return;
		}
		$this->assign('article', $article);

		/* 店铺信息 */
		$this->set_store($article['store_id']);
		$store = $this->get_store_data();
		$this->assign('store', $store);

		/* 当前位置 */
		$this->_curlocal(LANG :: get('all_stores'), 'index.php?app=search&amp;act=store', $store['store_name'], 'index.php?app=store&amp;id=' . $store['store_id'], $article['title']);

		$this->_config_seo('title', $article['title'] . ' - ' . $store['store_name']);
		$this->display('store.article.html');
	}

	/* 信用评价 */
	function credit() {
		/* 店铺信息 */
		$id = empty ($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('Hacking Attempt');
			return;
		}
		$this->set_store($id);
		$store = $this->get_store_data();
		$this->assign('store', $store);
		/* 取得评价过的商品 */
		if (!empty ($_GET['eval']) && in_array($_GET['eval'], array (
				1,
				2,
				3
			))) {
			$conditions = "AND evaluation = '{$_GET['eval']}'";
		} else {
			$conditions = "";
			$_GET['eval'] = '';
		}
		$page = $this->_get_page(10);
		$order_goods_mod = & m('ordergoods');
		$goods_list = $order_goods_mod->find(array (
			'conditions' => "seller_id = '$id' AND evaluation_status = 1 AND is_valid = 1 " . $conditions,
			'join' => 'belongs_to_order',
			'fields' => 'buyer_id, buyer_name, anonymous, evaluation_time, goods_id, goods_name, specification, price, quantity, goods_image, evaluation, comment',
			'order' => 'evaluation_time desc',
			'limit' => $page['limit'],
			'count' => true,

		));
		$this->assign('goods_list', $goods_list);

		$page['item_count'] = $order_goods_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);

		/* 按时间统计 */
		$stats = array ();
		for ($i = 0; $i <= 3; $i++) {
			$stats[$i]['in_a_week'] = 0;
			$stats[$i]['in_a_month'] = 0;
			$stats[$i]['in_six_month'] = 0;
			$stats[$i]['six_month_before'] = 0;
			$stats[$i]['total'] = 0;
		}

		$goods_list = $order_goods_mod->find(array (
			'conditions' => "seller_id = '$id' AND evaluation_status = 1 AND is_valid = 1 ",
			'join' => 'belongs_to_order',
			'fields' => 'evaluation_time, evaluation',

		));
		foreach ($goods_list as $goods) {
			$eval = $goods['evaluation'];
			$stats[$eval]['total']++;
			$stats[0]['total']++;

			$days = (gmtime() - $goods['evaluation_time']) / (24 * 3600);
			if ($days <= 7) {
				$stats[$eval]['in_a_week']++;
				$stats[0]['in_a_week']++;
			}
			if ($days <= 30) {
				$stats[$eval]['in_a_month']++;
				$stats[0]['in_a_month']++;
			}
			if ($days <= 180) {
				$stats[$eval]['in_six_month']++;
				$stats[0]['in_six_month']++;
			}
			if ($days > 180) {
				$stats[$eval]['six_month_before']++;
				$stats[0]['six_month_before']++;
			}
		}
		$this->assign('stats', $stats);

		/* 当前位置 */
		$this->_curlocal(LANG :: get('all_stores'), 'index.php?app=search&amp;act=store', $store['store_name'], 'index.php?app=store&amp;id=' . $store['store_id'], LANG :: get('credit_evaluation'));

		$this->_config_seo('title', Lang :: get('credit_evaluation') . ' - ' . $store['store_name']);
		$this->display('store.credit.html');
	}

	/* 取得友情链接 */
	function _get_partners($id) {
		$partner_mod = & m('partner');
		return $partner_mod->find(array (
			'conditions' => "store_id = '$id'",
			'order' => 'sort_order',

		));
	}

	/* 取得推荐商品 */
	function _get_recommended_goods($id, $num = 12) {
		$goods_mod = & bm('goods', array (
			'_store_id' => $id
		));
		$goods_list = $goods_mod->find(array (
			'conditions' => "closed = 0 AND if_show = 1 AND recommended = 1",
			'fields' => 'goods_name, default_image, price',
			'limit' => $num,

		));
		foreach ($goods_list as $key => $goods) {
			empty ($goods['default_image']) && $goods_list[$key]['default_image'] = Conf :: get('default_goods_image');
		}

		return $goods_list;
	}

	/* 取得推荐商品 */
	function get_recommended_goods($id, $num = 12) {
		$goods_mod = & bm('goods', array (
			'_store_id' => $id
		));
		$goods_list = $goods_mod->find(array (
			'conditions' => "closed = 0 AND if_show = 1 AND recommended = 1",
			'fields' => 'goods_name, default_image, price',
		));
		foreach ($goods_list as $key => $goods) {
			empty ($goods['default_image']) && $goods_list[$key]['default_image'] = Conf :: get('default_goods_image');
		}
		return $goods_list;
	}

	function _get_new_groupbuy($id, $num = 12) {
		$model_groupbuy = & m('groupbuy');
		$groupbuy_list = $model_groupbuy->find(array (
			'fields' => 'goods.default_image, this.group_name, this.group_id, this.spec_price, this.end_time',
			'join' => 'belong_goods',
		'conditions' => $model_groupbuy->getRealFields('this.state=' . GROUP_ON . ' AND this.store_id=' . $id . ' AND end_time>' . gmtime()), 'order' => 'group_id DESC', 'limit' => $num));
		if (empty ($groupbuy_list)) {
			$groupbuy_list = array ();
		}
		foreach ($groupbuy_list as $key => $_g) {
			empty ($groupbuy_list[$key]['default_image']) && $groupbuy_list[$key]['default_image'] = Conf :: get('default_goods_image');
			$tmp = current(unserialize($_g['spec_price']));
			$groupbuy_list[$key]['price'] = $tmp['price'];
			$groupbuy_list[$key]['lefttime'] = lefttime($_g['end_time']);
		}

		return $groupbuy_list;
	}

	/* 取得最新商品 */
	function _get_new_goods($id, $num = 12) {
		$goods_mod = & bm('goods', array (
			'_store_id' => $id
		));
		$goods_list = $goods_mod->find(array (
			'conditions' => "closed = 0 AND if_show = 1",
			'fields' => 'goods_name, default_image, price',
			'order' => 'add_time desc',
			'limit' => $num,

		));
		foreach ($goods_list as $key => $goods) {
			empty ($goods['default_image']) && $goods_list[$key]['default_image'] = Conf :: get('default_goods_image');
		}

		return $goods_list;
	}

	/* 取得最新商品 */
	function get_new_goods($id, $num = 12) {
		$goods_mod = & bm('goods', array (
			'_store_id' => $id
		));
		$goods_list = $goods_mod->find(array (
			'conditions' => "closed = 0 AND if_show = 1",
			'fields' => 'goods_name, default_image, price',
			'order' => 'add_time desc',
		));
		foreach ($goods_list as $key => $goods) {
			empty ($goods['default_image']) && $goods_list[$key]['default_image'] = Conf :: get('default_goods_image');
		}

		return $goods_list;
	}

	/* 取得最新商品 */
	function _get_newt_recommended_goods($id, $num = 12) {
		$goods_mod = & bm('goods', array (
			'_store_id' => $id
		));
		$goods_list = $goods_mod->find(array (
			'conditions' => "closed = 0 AND if_show = 1  AND recommended = 1",
			'fields' => 'goods_name, default_image, price',
			'order' => 'add_time desc',
			'limit' => $num,

		));
		foreach ($goods_list as $key => $goods) {
			empty ($goods['default_image']) && $goods_list[$key]['default_image'] = Conf :: get('default_goods_image');
		}

		return $goods_list;
	}

    /* 取得最新商品 */
	function get_newt_recommended_goods($id) {
		$goods_mod = & bm('goods', array (
			'_store_id' => $id
		));
		$goods_list = $goods_mod->find(array (
			'conditions' => "closed = 0 AND if_show = 1  AND recommended = 1",
			'fields' => 'goods_name, default_image, price',
			'order' => 'add_time desc',
		));
		foreach ($goods_list as $key => $goods) {
			empty ($goods['default_image']) && $goods_list[$key]['default_image'] = Conf :: get('default_goods_image');
		}

		return $goods_list;
	}

	/* 搜索到的结果 */
	function _assign_searched_goods($id = "", $cate_id = "") {
		$goods_mod = & bm('goods', array (
			'_store_id' => $id
		));
		$search_name = LANG :: get('all_goods');

		$conditions = $this->_get_query_conditions(array (
			array (
				'field' => 'goods_name',
				'name' => 'keyword',
				'equal' => 'like',

			),

		));
		if ($conditions) {
			$search_name = sprintf(LANG :: get('goods_include'), $_GET['keyword']);
			$sgcate_id = 0;
		} else {
			$sgcate_id = empty ($_GET['cate_id']) ? 0 : intval($_GET['cate_id']);
		}

		if ($sgcate_id > 0) {
			$gcategory_mod = & bm('gcategory', array (
				'_store_id' => $id
			));
			$sgcate = $gcategory_mod->get_info($sgcate_id);
			$search_name = $sgcate['cate_name'];

			$sgcate_ids = $gcategory_mod->get_descendant_ids($sgcate_id);
		} else {
			$sgcate_ids = array ();
		}

		/* 排序方式 */
		$orders = array (
			'add_time desc' => LANG :: get('add_time_desc'
		), 'price asc' => LANG :: get('price_asc'), 'price desc' => LANG :: get('price_desc'),);
		$this->assign('orders', $orders);

		$page = $this->_get_page(16);
		       $goods_list = $goods_mod->get_list(array(
		            'conditions' => 'closed = 0 AND if_show = 1' . $conditions,
		            'count' => true,
		            'order' => empty($_GET['order']) || !isset($orders[$_GET['order']]) ? 'add_time desc' : $_GET['order'],
		            'limit' => $page['limit'],
		        ), $sgcate_ids);
//		if (empty ($cate_id)) { //查询店铺中的所有菜品
//			$goods_list = $goods_mod->get_storegoods($id);
//		} else { //查询店铺指定类型的菜品
//			$goods_list = $goods_mod->get_categoods($cate_id);
//		}
		foreach ($goods_list as $key => $goods) {
			empty ($goods['default_image']) && $goods_list[$key]['default_image'] = Conf :: get('default_goods_image');
		}
		$this->assign('searched_goods', $goods_list);
        $this->assign('searched_allgoods', 1);
		$page['item_count'] = $goods_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);

		$this->assign('search_name', $search_name);
	}

	/**
	 * 取得文章信息
	 */
	function _get_article($id) {
		$article_mod = & m('article');
		return $article_mod->get_info($id);
	}

	function _get_seo_info($data) {
		$seo_info = $keywords = array ();
		$seo_info['title'] = $data['store_name'] . ' - ' . Conf :: get('site_title');
		$keywords = array (
			str_replace("\t",
			' ',
			$data['region_name']
		), $data['store_name'],);
		//$seo_info['keywords'] = implode(',', array_merge($keywords, $data['tags']));
		$seo_info['keywords'] = implode(',', $keywords);
		$seo_info['description'] = sub_str(strip_tags($data['description']), 10, true);
		return $seo_info;
	}
}
?>
